﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using EcSalseManage.App_Code;

namespace EcSalseManage.EcSalse
{
    public partial class Report_Change_AreaUnit_Detail : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!CommonFunction.isInPrev("Report_AreaUnit_0001"))
                {
                    Response.Redirect("../Error/Perm_Error.aspx");
                }
                if (!CommonFunction.isInPrev("Report_AreaUnit_0002"))
                {
                    this.btnSave.Enabled = false;
                }
                if (!CommonFunction.isInPrev("Report_AreaUnit_0003"))
                {
                    this.btnDelete.Enabled = false;
                }


                DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit Report_Change_AreaUnit_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit();
                DataSet ds = Report_Change_AreaUnit_Inherit.FindLibrary();
                this.chklReport_Adjust.DataSource = ds.Tables[0];
                this.chklReport_Adjust.DataValueField = "Report_Adjust_ID";
                this.chklReport_Adjust.DataTextField = "Report_Adjust_Name";
                this.chklReport_Adjust.DataBind();

     
                if (Request.QueryString["typeid"].ToString() == "7")
                {
                    this.lblTitle.Text = "变更面积";
                }
                else if (Request.QueryString["typeid"].ToString() == "8")
                {
                    this.lblTitle.Text = "当月减成交价";
                }

                this.Report_Tab1.ExchangeTypeID = Request.QueryString["typeid"].ToString();
                if (Request.QueryString["id"] != null)
                {
                    ViewState["id"] = Request.QueryString["id"].ToString();
                    Update_Data_Bind(ViewState["id"].ToString());
                    btnReset.Visible = false;
                    btnDelete.Visible = true;
                    this.Report_Tab1.MainID = ViewState["id"].ToString();
                    this.Report_Tab1.Visible = true;
                }
                else if(Session["ProjectID"]!=null)
                {
                    DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
                    this.lblProjectName.Text = Project_Inherit.getProjectName(Session["ProjectID"].ToString());
                    this.btnAudit.Visible = false;
                    ViewState["id"] = null;
                }
            }
        }


        private void Update_Data_Bind(string id)
        {
            DataAccess.Operate.DA_Report_Change_AreaUnit_Operate Report_Change_AreaUnit_Operate = new DataAccess.Operate.DA_Report_Change_AreaUnit_Operate();
            DataEntity.T_Report_Change_AreaUnit Report_Change_AreaUnit = new DataEntity.T_Report_Change_AreaUnit();
            Report_Change_AreaUnit = (DataEntity.T_Report_Change_AreaUnit)Report_Change_AreaUnit_Operate.SelectOne(id);
            this.txtReport_Change_AreaUnit_AdjustReason.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_AdjustReason.ToString();
            this.txtReport_Change_AreaUnit_ExchangeDate.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_ExchangeDate.ToString();
            this.txtReport_Change_AreaUnit_FormlyerData.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerData.ToString();
            this.txtReport_Change_AreaUnit_FormlyerExchangeDate.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerExchangeDate.ToString();
            this.txtReport_Change_AreaUnit_FormlyerReportNO.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerReportNO.ToString();
            this.txtReport_Change_AreaUnit_NowData.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_NowData.ToString();
            this.txtReport_Change_AreaUnit_ReduceData.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_ReduceData.ToString();
            this.txtReport_Change_AreaUnit_Unit.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_Unit.ToString();
            this.txtReport_Change_AreaUnit_UpdateData.Text = Report_Change_AreaUnit.Report_Change_AreaUnit_UpdateData.ToString();

            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            this.lblProjectName.Text = Project_Inherit.getProjectName(Report_Change_AreaUnit.Report_Change_AreaUnit_ProjectID.ToString());
            ViewState["ProjectID"] = Report_Change_AreaUnit.Report_Change_AreaUnit_ProjectID.ToString();


            if (Report_Change_AreaUnit.Report_Plan_Audit > 28)
            {
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }

            if (Report_Change_AreaUnit.Report_Plan_Audit == 28)
                this.btnAudit.Text = "项经审核";
            if (Report_Change_AreaUnit.Report_Plan_Audit == 29)
                this.btnAudit.Text = "营销[副]总监审核";
            if (Report_Change_AreaUnit.Report_Plan_Audit == 30)
            {
                this.btnAudit.Text = "审核已完成";
                this.btnAudit.Enabled = false;
            }

            if (!CommonFunction.isInPrev("Report_AreaUnit_0005") && this.btnAudit.Text == "项经审核")
            {
                this.btnAudit.Enabled = false;
            }

            if (!CommonFunction.isInPrev("Report_AreaUnit_0006") && this.btnAudit.Text == "营销[副]总监审核")
            {
                this.btnAudit.Enabled = false;
            }


            DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit Report_Change_AreaUnit_Adjust_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit();
            DataSet ds = Report_Change_AreaUnit_Adjust_Inherit.FindAreaUnitAdjusts(id);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < this.chklReport_Adjust.Items.Count; j++)
                {
                    if (chklReport_Adjust.Items[j].Value == ds.Tables[0].Rows[i][0].ToString())
                    {
                        chklReport_Adjust.Items[j].Selected = true;
                    }
                }
            }


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

     
            DataAccess.Operate.DA_Report_Change_AreaUnit_Operate Report_Change_AreaUnit_Operate = new DataAccess.Operate.DA_Report_Change_AreaUnit_Operate();
            DataEntity.T_Report_Change_AreaUnit Report_Change_AreaUnit = new DataEntity.T_Report_Change_AreaUnit();
            Guid gReport_ID = System.Guid.NewGuid();

            Report_Change_AreaUnit.Report_Change_AreaUnit_AdjustReason = this.txtReport_Change_AreaUnit_AdjustReason.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_ExchangeDate = Convert.ToDateTime(this.txtReport_Change_AreaUnit_ExchangeDate.Text.Trim());
            Report_Change_AreaUnit.Report_Change_AreaUnit_ExchangeTypeID = Convert.ToInt32(this.Report_Tab1.ExchangeTypeID);
            Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerData = this.txtReport_Change_AreaUnit_FormlyerData.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerExchangeDate = Convert.ToDateTime(this.txtReport_Change_AreaUnit_FormlyerExchangeDate.Text.Trim());
            Report_Change_AreaUnit.Report_Change_AreaUnit_FormlyerReportNO = this.txtReport_Change_AreaUnit_FormlyerReportNO.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_NowData = this.txtReport_Change_AreaUnit_NowData.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_ReduceData = this.txtReport_Change_AreaUnit_ReduceData.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_Unit = this.txtReport_Change_AreaUnit_Unit.Text.Trim();
            Report_Change_AreaUnit.Report_Change_AreaUnit_UpdateData = this.txtReport_Change_AreaUnit_UpdateData.Text.Trim();
            Report_Change_AreaUnit.Report_Plan_Audit = 28;



            if (ViewState["id"] == null)
            {
                Report_Change_AreaUnit.Report_Change_AreaUnit_ID = gReport_ID;
                Report_Change_AreaUnit.Report_Change_AreaUnit_ProjectID = new Guid(Session["ProjectID"].ToString());
                if (Report_Change_AreaUnit_Operate.Insert(Report_Change_AreaUnit))
                {
                    DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Operate Report_Change_AreaUnit_Adjust_Operate = new DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Operate();
                    DataEntity.T_Report_Change_AreaUnit_Adjust Report_Change_AreaUnit_Adjust = new DataEntity.T_Report_Change_AreaUnit_Adjust();
                    Report_Change_AreaUnit_Adjust.Report_Change_AreaUnit_MainID = gReport_ID;
                    for (int i = 0; i < chklReport_Adjust.Items.Count; i++)
                    {
                        if (chklReport_Adjust.Items[i].Selected)
                        {
                            Report_Change_AreaUnit_Adjust.Report_Change_AreaUnit_AdjustID = Convert.ToInt32(chklReport_Adjust.Items[i].Value);
                            Report_Change_AreaUnit_Adjust_Operate.Insert(Report_Change_AreaUnit_Adjust);
                        }
                    }
                    
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, gReport_ID, 1);
                    Response.Write("<script language='javascript'>alert('" + Common_Const.MSG_ALL_INSERT_SUCCESS+ "');</script>");
                    //提醒项经审批
                    SendMessage("10", Session["ProjectID"].ToString());
                 
                }
                else
                {
                    Response.Write("<script>alert('" + Common_Const.MSG_ALL_INSERT_FAILS+ "');</script>");
                }
            }
            else
            {

                Report_Change_AreaUnit.Report_Change_AreaUnit_ID = new Guid(ViewState["id"].ToString());
                if (Report_Change_AreaUnit_Operate.Update(Report_Change_AreaUnit))
                {
                    DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit Report_Change_AreaUnit_Adjust_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit();
                    Report_Change_AreaUnit_Adjust_Inherit.DeleteByMainID(ViewState["id"].ToString());

                    DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Operate Report_Change_AreaUnit_Adjust_Operate = new DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Operate();
                    DataEntity.T_Report_Change_AreaUnit_Adjust Report_Change_AreaUnit_Adjust = new DataEntity.T_Report_Change_AreaUnit_Adjust();
                    Report_Change_AreaUnit_Adjust.Report_Change_AreaUnit_MainID = new Guid(ViewState["id"].ToString());
                    for (int i = 0; i < chklReport_Adjust.Items.Count; i++)
                    {
                        if (chklReport_Adjust.Items[i].Selected)
                        {
                            Report_Change_AreaUnit_Adjust.Report_Change_AreaUnit_AdjustID = Convert.ToInt32(chklReport_Adjust.Items[i].Value);
                            Report_Change_AreaUnit_Adjust_Operate.Insert(Report_Change_AreaUnit_Adjust);
                        }
                    }
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 2);
                    Response.Write("<script language='javascript'>alert('" + Common_Const.MSG_ALL_UPDATE_SUCCESS+ "');</script>");

                }
                else
                {
                    Response.Write("<script language='javascript'>alert('" + Common_Const.MSG_ALL_UPDATE_FAILS+ "');</script>");
                }

            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Change_AreaUnit_Operate Report_Change_AreaUnit_Operate = new DataAccess.Operate.DA_Report_Change_AreaUnit_Operate();
            if (Report_Change_AreaUnit_Operate.Delete(ViewState["id"].ToString()))
            {
                DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit Report_Change_AreaUnit_Adjust_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Adjust_Inherit();
                Report_Change_AreaUnit_Adjust_Inherit.DeleteByMainID(ViewState["id"].ToString());
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 3);
                Response.Write("<script>alert('" + Common_Const.MSG_ALL_DELETE_SUCCESS+ "');window.close();</script>");
         
            }
            else
            {
                Response.Write("<script>alert('" + Common_Const.MSG_ALL_DELETE_FAILS+ "');</script>");
            }
        }

        private void ClearForm()
        {
            Control frm = FindControl("Form1");
            foreach (Control ctrl in frm.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = " ";
                }
            }
            this.chklReport_Adjust.SelectedItem.Selected = false;
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.btnAudit.Text == "项经审核")
            {
               
                DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit Report_Change_AreaUnit_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit();
                if (Report_Change_AreaUnit_Inherit.AuditByID(ViewState["id"].ToString(), "29"))
                {
                 
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 10);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                    //提醒营销副总监审批
                    SendMessage("5", ViewState["ProjectID"].ToString());
                }
            }
            else if (this.btnAudit.Text == "营销[副]总监审核")
            {
               
                DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit Report_Change_AreaUnit_Inherit = new DataAccess.Operate.DA_Report_Change_AreaUnit_Inherit();
                if (Report_Change_AreaUnit_Inherit.AuditByID(ViewState["id"].ToString(), "30"))
                {
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 5);
                    Response.Write("<script>alert('审核成功');window.close();</script>");
                }
            }
        }


        public void SendMessage(string operate, string project)
        {
            string projectNo = "";
            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            string[] array = Project_Inherit.getCodeAndName(out projectNo, operate, project);
            string sTitle = "请注意，项目编号:" + projectNo + "中的变更面积需要您的审批。";
            string sBody = "请注意，项目编号:" + projectNo + " 中的变更面积需要您的审批。<a href='http://wap.gzcentaline.com.cn'>点击此处</a>";
            // array[1] 姓名 ，array[0] 工号
            EcSalseManage.App_Code.CommonFunction.SendMessage(array[1], array[0], sTitle, sBody);
        }



    }
}